# -*- coding: utf-8 -*-
"""
-------------------------------------------------
Project Name: luffcity study project
File Name: fileoperate.py
Author: kij
Create Date: 2021/3/1
-------------------------------------------------
"""
import os
import hashlib
import configparser
from conn import setting

class User(object):
	"""
	用户注册
	"""

	def __init__(self, username, password,size):
		self.username = username
		self.size = size
		self.password = hashlib.md5(password.encode('utf-8')).hexdigest()

	def register_user(self, save_path):
		config = configparser.ConfigParser()
		config.read(save_path)
		if not config.has_section(self.username):
			# 用户不存在
			config.add_section(self.username)
			# 添加键值和内容
			config.set(self.username, 'username', self.username)
			config.set(self.username, 'password', self.password)
			config.set(self.username, 'size',str( int(self.size)*1024**2))
			config.write(open(save_path, mode='w', encoding='utf-8'))

			folder_path = os.path.join(setting.BASE_DIR, 'web', 'home', self.username)
			print("文件夹=", folder_path)
			if not os.path.exists(folder_path):
				os.makedirs(folder_path)
			return True, '注册成功'
		else:
			return False, "用户已存在"


